<!-- 
All rights Reserved, Designed By www.youyacao.com 
@Description:广告页文件
@author:成都市一颗优雅草科技有限公司     
@version V5.x 
注意：本前端源码遵循 MulanPSL-2.0开源协议（木兰宽松许可证）本内容仅限于个人参考，禁止用于其他的商业用途
需要商业用途或者定制开发等可访问songshu.youyacao.com   企业客服QQ:2853810243  官方客户·技术交流群 929353806交流群 929353806
 -->
<template>
	<view class="body">
		<image @tap="download" class="advert-img" :style="{'height': `${windowHeight}px`}" :src="advert.home!=undefined?formatUrl(advert.home.img):''"
		 mode="aspectFill" @load="_load"></image>
		<text class="goto-time-text" @tap="toHome">倒计时{{time}}s</text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				interval: null,
				windowHeight: 0,
				time: 4
			}
		},
		onLoad() {
			this.windowHeight = uni.getSystemInfoSync().windowHeight
		},
		methods: {
			_load() {
				this.startTime()
			},
			//格式化链接地址
			formatUrl(url) {
				if (url == undefined) {
					return "";
				}
				var index = url.indexOf("http");
				if (index == 0) {
					return url;
				}
				return this.$store.state.baseUrl + url;
			},
			download() {
				//当前并无广告，直接返回
				if (this.advert.home == undefined) {
					return;
				}
				//有广告，判断类型
				if (this.advert.home.open_type == 1) {
					//调用下载
					this.$store.dispatch('downloadAndInstall', this.advert.home.ad_url);
				} else if (this.advert.home.open_type == 2) {
					plus.runtime.openURL(this.advert.home.ad_url);
				}
			},
			toHome() {
				uni.redirectTo({
					url: '/pages/index/index',
				})
			},
			startTime() {
				this.interval = setInterval(() => {
					this.time--
					if (this.time < 0) {
						clearInterval(this.interval)
						this.toHome()
					}
				}, 1000)
			}
		},
		computed: {
			advert() {
				return this.$store.state.advert;
			}
		}
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
	}

	.body {
		position: relative;
		width: 100%;
		height: 100%;
	}

	.advert-img {
		width: 750rpx;
	}

	.goto-time-text {
		padding: 0 24rpx;
		font-size: 22upx;
		position: absolute;
		right: 32upx;
		top: 84upx;
		height: 25upx;
		color: #eeeeee;
		line-height: 25upx;
		padding: 13upx;
		border-radius: 6rpx;
		background-color: rgba(0, 0, 0, .5);
	}
</style>
